﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Data.OracleClient;


namespace Xiaov.SmartWeb.Application.HXZB
{
    public partial class NhzbRightDownData : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                #region 获取参数
                string zbbm = "", dwbm = "", nf = "", yf = "",zbxz="", type = "";
                if (Request.QueryString["zbbm"] != null)
                {
                    zbbm = Request.QueryString["zbbm"].ToString();
                }
                if (Request.QueryString["zbxz"] != null)
                {
                    zbxz = Request.QueryString["zbxz"].ToString();
                }
                if (Request.QueryString["dwbm"] != null)
                {
                    dwbm = Request.QueryString["dwbm"].ToString();
                }
                if (Request.QueryString["nf"] != null && Request.QueryString["yf"] != null)
                {
                    nf = Request.QueryString["nf"].ToString();
                    yf = Request.QueryString["yf"].ToString();
                }
                if (Request.QueryString["type"] != null)//数据信息 标题，本年数据，去年数据
                {
                    type = Request.QueryString["type"].ToString();
                }
                #endregion
                string bbbm = "NH63";
                string zdmc = "XHHJ";
                switch (zbbm)
                {
                    case "NH6101"://电力
                        bbbm = "NH63";
                        zdmc = "XHHJ";
                        break;
                    case "NH6102"://原油
                        bbbm = "NH64";
                        zdmc = "YYHJ";
                        break;
                    case "NH6104"://天然气
                        bbbm = "NH65";
                        zdmc = "XHHJ";
                        break;
                    case "NH6105"://汽油
                        bbbm = "NH66";
                        zdmc = "QYHJ";
                        break;
                    case "NH6106"://柴油
                        bbbm = "NH66";
                        zdmc = "CYHJ";
                        break;
                    default:
                        break;
                }
                zbbm = bbbm + getDwbm(zbxz);
                StringBuilder sbSql = new StringBuilder();
                sbSql.AppendFormat(@"select b.yf,a.bnlj,b.bnlj qntqlj from (select {0} bnlj,yf from {1} where nf={2} and yf<={3} and zbbm='{4}' and dwbm='{5}') a,
                (select {0} bnlj,yf from {1} where nf={2}-1 and yf<={3} and zbbm='{4}' and dwbm='{5}') b where a.yf(+)=b.yf order by to_number(b.yf)",
                     zdmc, bbbm, nf, "12", zbbm, dwbm);
                DataSet ds = Maticsoft.DBUtility.DbHelperOra.Query(sbSql.ToString());
                #region 输出数据
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        if (ds.Tables[0].Rows[i]["bnlj"].ToString() == "")
                        {
                            ds.Tables[0].Rows[i]["bnlj"] = 0;
                        }
                        if (ds.Tables[0].Rows[i]["qntqlj"].ToString() == "")
                        {
                            ds.Tables[0].Rows[i]["qntqlj"] = 0;
                        }
                        if (type == "title")
                        {
                            if (i != ds.Tables[0].Rows.Count - 1)
                            {
                                Response.Write(ds.Tables[0].Rows[i]["yf"].ToString() + "月\t");
                            }
                            else
                            {
                                Response.Write(ds.Tables[0].Rows[i]["yf"].ToString() + "月");
                            }
                        }
                        else if (type == "bnlj")
                        {
                            if (i != ds.Tables[0].Rows.Count - 1)
                            {
                                Response.Write(ds.Tables[0].Rows[i]["bnlj"].ToString() + "\t");
                            }
                            else
                            {
                                Response.Write(ds.Tables[0].Rows[i]["bnlj"].ToString());
                            }
                        }
                        else if (type == "qntq")
                        {
                            if (i != ds.Tables[0].Rows.Count - 1)
                            {
                                Response.Write(ds.Tables[0].Rows[i]["qntqlj"].ToString() + "\t");
                            }
                            else
                            {
                                Response.Write(ds.Tables[0].Rows[i]["qntqlj"].ToString());
                            }
                        }
                    }
                    Response.End();
                }
                #endregion               
            }
        }

        /// <summary>
        /// 获取单位编码
        /// </summary>
        /// <param name="zbmc"></param>
        /// <returns></returns>
        protected string getDwbm(string zbmc)
        {
            string dwbm = "";
            string sql = "select dwbm from dwbmb where dwmc='" + zbmc + "'";
            OracleDataReader dr = (OracleDataReader)Maticsoft.DBUtility.DbHelperOra.ExecuteReader(sql);
            if (dr.Read())
            {
                dwbm = dr["dwbm"].ToString();
            }
            dr.Close();
            return dwbm;
        }
    }
}